// 通过中间件方式来收集依赖，实现解耦，减少维护成本


class A {
    name: string
    constructor(name: string){
        this.name = name;
    }
}

class C {
    name: string
    constructor(name: string){
        this.name = name;
    }
}

// 中间件用于解耦
class Container {
    modules: any
    constructor () {
        this.modules = {};
    }
    
    provide(key: string, modules: any){
        this.modules[key] = modules;
    }
    
    get(key:string){
        return this.modules[key];
    }
}

// 实例化中间件类，依赖注入
const mo = new Container();
mo.provide('a', new A('test1'));
mo.provide('c', new C('test2'));

class B {
    a: any
    c:any
    constructor(container: Container){
        this.a = container.get('a');
        this.c = container.get('c');
    }
}

const b = new B(mo);

console.log(b.a.name);
console.log(b.c.name);
